package com.qf.springbootproject.handler;

import cn.hutool.jwt.JWT;
import com.qf.springbootproject.config.Constants;
import com.qf.springbootproject.utils.ResultVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

//@Component
public class CheckTokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getParameter("token");
        if (token == null) {
            return false;
        } else {
            //校验token的合法性
            boolean validate = JWT.of(token).setKey(Constants.SECRET.getBytes(StandardCharsets.UTF_8)).validate(0);
            if (validate) {
                //token合法
                return true;

            } else {
                //token不合法
                return false;
            }
        }

    }
}
